package Search.a03BlockSearchdemo3;

public class Test1 {
    public static void main(String[] args) {
        int[] arr = {16, 5, 9, 12, 21, 18,
                32, 23, 37, 26, 45, 34,
                50, 48, 61, 52, 73, 66};

        block1 b1 = new block1(21, 0, 5);
        block1 b2 = new block1(45, 6, 11);
        block1 b3 = new block1(73, 12, 17);

        block1[] blockArr = {b1, b2, b3};
        int number = 32;
        int index = getIndex(blockArr, arr, number);
        System.out.println(index);
    }

    private static int getIndex(block1[] blockArr, int[] arr, int number) {
        int indexBlock = findIndexBlock(blockArr, number);
        if (indexBlock == -1) {
            return -1;
        }

        int startIndex = blockArr[indexBlock].getStartindex();
        int endIndex = blockArr[indexBlock].getEndindex();

        for (int i = startIndex; i <= endIndex; i++) {
            if (arr[i] == number) {
                return i;
            }
        }
        return -1;
    }

    public static int findIndexBlock(block1[] blockArr, int number) {
        for (int i = 0; i < blockArr.length; i++) {
            if (number <= blockArr[i].getMax()) {
                return i;
            }
        }
        return -1;
    }
}

class block1 {
    private int max;
    private int startindex;
    private int endindex;

    public block1() {
    }

    public block1(int max, int startindex, int endindex) {
        this.max = max;
        this.startindex = startindex;
        this.endindex = endindex;
    }


    public int getMax() {
        return max;
    }


    public void setMax(int max) {
        this.max = max;
    }


    public int getStartindex() {
        return startindex;
    }

    public void setStartindex(int startindex) {
        this.startindex = startindex;
    }


    public int getEndindex() {
        return endindex;
    }


    public void setEndindex(int endindex) {
        this.endindex = endindex;
    }

    public String toString() {
        return "block{max = " + max + ", startindex = " + startindex + ", endindex = " + endindex + "}";
    }
}